package algorithm;

import java.util.Arrays;

//动态规划解决0-1背包问题
public class Dp {

    public static void main(String[] args) {
        int[] w = {1, 4, 3};
        int[] val = {1500, 3000, 2000};
        int m = 4;//背包的容量
        int n = val.length;//物品的个数
        int[][] v = new int[n + 1][m + 1];

        //动态规划处理0-1背包问题
        for (int i = 1; i < v.length; i++) {
            for (int j = 1; j < v[0].length; j++) {
                if (w[i - 1] > j) {
                    v[i][j] = v[i - 1][j];
                } else {
                    v[i][j]=Math.max(v[i-1][j],val[i-1]+v[i-1][j-w[i-1]]);
                }
            }
        }

        for (int[] i:v){
            System.out.println(Arrays.toString(i));
        }

    }
}
